package com.mp.approvalreq.util;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.OutSignInfo;
import com.mp.approvalreq.entity.common.Constants;
import com.mp.approvalreq.entity.dto.TemplateDTO;
import com.mp.approvalreq.entity.message.RequestTimeMsg;
import com.mp.approvalreq.entity.message.TemplateMsg;
import com.mp.approvalreq.service.ApprovalOperateServicePrx;
import com.mp.approvalreq.service.CommontApprovalServicePrx;
import com.mp.approvalreq.service.SelectApprovalServicePrx;
import com.plusmoney.exception.RpcServerLogicalException;
import com.plusmoney.mq.MQProducer;
import com.plusmoney.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MQApprovalUtils {

    private static final Logger logger = LoggerFactory.getLogger(MQApprovalUtils.class);

    private SelectApprovalServicePrx selectApprovalService;
    private ApprovalOperateServicePrx approvalOperateService;
    private CommontApprovalServicePrx commontApprovalService;

    public MQApprovalUtils() {
    }

    public void initSelectApprovalService(SelectApprovalServicePrx selectApprovalService) {
        this.selectApprovalService = selectApprovalService;
    }

    public void initApprovalOperateService(ApprovalOperateServicePrx approvalOperateService) {
        this.approvalOperateService = approvalOperateService;
    }

    public void initCommontApprovalService(CommontApprovalServicePrx commontApprovalService) {
        this.commontApprovalService = commontApprovalService;
    }


    public void recordOutSignInfo(OutSignInfo outSignInfo) {
        try {
            commontApprovalService.recordOutAddress(outSignInfo);
        } catch (Throwable throwable) {
            logger.error(throwable.getMessage(), throwable);
        }
    }

    public void recordDaysOffTime(RequestTimeMsg requestTimeMsg) {
        check(requestTimeMsg);
        try {
            commontApprovalService.recordDaysOffTime(requestTimeMsg);

        } catch (Throwable throwable) {
            logger.error(throwable.getMessage(), throwable);
        }
    }

    private void check(RequestTimeMsg requestTimeMsg) {
        if (Utils.isNull(requestTimeMsg)) {
            throw new RpcServerLogicalException(1, "消息不能为空");
        }

        if (Utils.isNull(requestTimeMsg.getRequestId())) {
            throw new RpcServerLogicalException(1, "审批id不能为空");
        }

        if (Utils.isNull(requestTimeMsg.getDaysOffTimes())) {
            throw new RpcServerLogicalException(1, "调休时长不能为空");
        }
    }

    public void switchTemplateTempCc(TemplateMsg msg) {
        check(msg);
        try {
            TemplateDTO templateDTO = selectApprovalService.getTemplateDTO(msg.getCompanyId(), msg.getTemplateId());

            approvalOperateService.switchTemplateTempCc(templateDTO.getAncestorId(), msg.getOperateFlag());
        } catch (Throwable throwable) {
            logger.error(throwable.getMessage(), throwable);
        }
    }

    private static void check(TemplateMsg msg) {
        if (Utils.isNull(msg)) {
            throw new RpcServerLogicalException(13003, "参数错误");
        }

        if (Utils.isNull(msg.getCompanyId())) {
            throw new RpcServerLogicalException(13004, "公司id不能为空");
        }

        if (Utils.isNull(msg.getTemplateId())) {
            throw new RpcServerLogicalException(13005, "模板id不能为空");
        }

        if (Utils.isNull(msg.getOperateFlag())) {
            throw new RpcServerLogicalException(13006, "操作参数不能为空");
        }
    }


    public static void sendHrCalculateDaysOff(Integer companyId, Integer companyInfoId) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("companyId", companyId);
            jsonObject.put("companyInfoId", companyInfoId);

            MQProducer.send(Constants.MQ_HR_TOPIC, Constants.MQ_TAGS_RECALCULATE_DAYS_OFF_TIME, jsonObject);
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
            MailUtils.sendMail(MailUtils.EMAIL, "通知人事结算调休时长出错", "审批消费者");
        }
    }


}
